<?php

require_once 'core.php';
require_once cf\Config::path.'shop.php';

$tpl = 'edit_image';

function do_edit_save($aid, $params) {
	$iId = cf\api\admin\doAction($aid, $params);
	if (array_key_exists('id',$params)) {
		$iId = $params['id'];
	}
	
			//update albums
	$pos = cf\query2arrays('SELECT album_id,sort_order FROM cf_gallery_image_in_album WHERE image_id=:id',array('id'=>$iId),false,'album_id');
	cf\execQuery('DELETE FROM cf_gallery_image_in_album WHERE image_id=:id',array('id'=>$iId));
	$albums = cf\param::exist('album') ? cf\param::get('album') : array();
	$q = cf\createQuery('INSERT INTO cf_gallery_image_in_album (album_id,image_id,sort_order) VALUES(:albumId,:imageId,:sortOrder)',array('imageId'=>$iId));
	foreach ($albums as $albumId) {
		$q->setParam('albumId',$albumId);
		$q->setParam('sortOrder', array_key_exists($albumId,$pos) ? $pos[$albumId]['sort_order'] : 500);
		$q->execute();
	}
}

require_once 'edit.php';
?>